/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, CMOSTEK SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * Copyright (C) CMOSTEK SZ.
 */

/*!
 * @file    radio.c
 * @brief   Generic radio handlers
 *
 * @version 1.2
 * @date    Jul 17 2017
 * @author  CMOSTEK R@D
 */
 
#include "radio.h"
#include "cmt2300a_params.h"

#include <string.h>


void RF_Init(uint8 rf_type)
{
    u8 tmp;
    
		CMT2300A_InitGpio();
		CMT2300A_Init();
    
    if(rf_type == FSK)
    {
      /* Config registers */
      CMT2300A_ConfigRegBank(CMT2300A_CMT_BANK_ADDR       , g_cmt2300aCmtBank       , CMT2300A_CMT_BANK_SIZE       );
      CMT2300A_ConfigRegBank(CMT2300A_SYSTEM_BANK_ADDR    , g_cmt2300aSystemBank    , CMT2300A_SYSTEM_BANK_SIZE    );
      CMT2300A_ConfigRegBank(CMT2300A_FREQUENCY_BANK_ADDR , g_cmt2300aFrequencyBank , CMT2300A_FREQUENCY_BANK_SIZE );
      CMT2300A_ConfigRegBank(CMT2300A_DATA_RATE_BANK_ADDR , g_cmt2300aDataRateBank  , CMT2300A_DATA_RATE_BANK_SIZE );
      CMT2300A_ConfigRegBank(CMT2300A_BASEBAND_BANK_ADDR  , g_cmt2300aBasebandBank  , CMT2300A_BASEBAND_BANK_SIZE  );
      CMT2300A_ConfigRegBank(CMT2300A_TX_BANK_ADDR        , g_cmt2300aTxBank        , CMT2300A_TX_BANK_SIZE        );
    }
    else if(rf_type == OOK)
    {
      /* Config registers */
      CMT2300A_ConfigRegBank(CMT2300A_CMT_BANK_ADDR       , g_cmt2300aCmtBank_OOK       , CMT2300A_CMT_BANK_SIZE       );
      CMT2300A_ConfigRegBank(CMT2300A_SYSTEM_BANK_ADDR    , g_cmt2300aSystemBank_OOK    , CMT2300A_SYSTEM_BANK_SIZE    );
      CMT2300A_ConfigRegBank(CMT2300A_FREQUENCY_BANK_ADDR , g_cmt2300aFrequencyBank_OOK , CMT2300A_FREQUENCY_BANK_SIZE );
      CMT2300A_ConfigRegBank(CMT2300A_DATA_RATE_BANK_ADDR , g_cmt2300aDataRateBank_OOK  , CMT2300A_DATA_RATE_BANK_SIZE );
      CMT2300A_ConfigRegBank(CMT2300A_BASEBAND_BANK_ADDR  , g_cmt2300aBasebandBank_OOK  , CMT2300A_BASEBAND_BANK_SIZE  );
      CMT2300A_ConfigRegBank(CMT2300A_TX_BANK_ADDR        , g_cmt2300aTxBank_OOK        , CMT2300A_TX_BANK_SIZE        );
    }
    
    // xosc_aac_code[2:0] = 2
    tmp = (~0x07) & CMT2300A_ReadReg(CMT2300A_CUS_CMT10);
    CMT2300A_WriteReg(CMT2300A_CUS_CMT10, tmp|0x02);
    
	  RF_Config(rf_type);
}

void RF_Config(uint8 type)
{
	 u8 nInt2Sel;
	
#ifdef ENABLE_ANTENNA_SWITCH
    /* If you enable antenna switch, GPIO1/GPIO2 will output RX_ACTIVE/TX_ACTIVE,
       and it can't output INT1/INT2 via GPIO1/GPIO2 */
    CMT2300A_EnableAntennaSwitch(0);
#endif
	
	   /* Config GPIOs */
	if(type == FSK)
    CMT2300A_ConfigGpio(CMT2300A_GPIO1_SEL_INT2);   /* INT2 > GPIO3 */    
	else
    CMT2300A_ConfigGpio(CMT2300A_GPIO1_SEL_DIN);   /* INT2 > GPIO3 */    
		
    /* Config interrupt */
    nInt2Sel=	CMT2300A_INT_SEL_TX_DONE; /* Config INT2 */
    nInt2Sel &= CMT2300A_MASK_INT2_SEL;
    nInt2Sel |= (~CMT2300A_MASK_INT2_SEL) & CMT2300A_ReadReg(CMT2300A_CUS_INT2_CTL);
    CMT2300A_WriteReg(CMT2300A_CUS_INT2_CTL, nInt2Sel); 
	
    /* Enable interrupt */
      CMT2300A_EnableInterrupt(
	                              CMT2300A_MASK_TX_DONE_EN 
//															CMT2300A_MASK_PKT_DONE_EN | 
//															CMT2300A_MASK_PREAM_OK_EN |
//															CMT2300A_MASK_SYNC_OK_EN  |
//															CMT2300A_MASK_NODE_OK_EN  |
//															CMT2300A_MASK_CRC_OK_EN   
	                             );      
//	CMT2300A_EnableInterrupt(
//        CMT2300A_MASK_TX_DONE_EN  |
//        CMT2300A_MASK_PKT_DONE_EN
//        );
//    if(type == OOK)
    {
      CMT2300A_WriteReg(CMT2300A_CUS_FIFO_CTL, 0xE0);
    }

    
    /* Disable low frequency OSC calibration */
    CMT2300A_EnableLfosc(FALSE);
    
    /* Use a single 64-byte FIFO for either Tx or Rx */
    //CMT2300A_EnableFifoMerge(TRUE);
    
    //CMT2300A_SetFifoThreshold(16); // FIFO_TH
    
    /* This is optional, only needed when using Rx fast frequency hopping */
    /* See AN142 and AN197 for details */
    //CMT2300A_SetAfcOvfTh(0x27);
    
    /* Go to sleep for configuration to take effect */
		
		
		
    CMT2300A_GoSleep();
}

code u8* TabPowerBank[] = {g_cmt2300aTxBank,g_cmt2300aTxBank_N9dBm,g_cmt2300aTxBank_N11dBm,g_cmt2300aTxBank_N13dBm};
u8 PowerSet;
void RF_PowerSet(u8 power)
{
    CMT2300A_GoStby();
    CMT2300A_ConfigRegBank(CMT2300A_TX_BANK_ADDR        , TabPowerBank[power]        , CMT2300A_TX_BANK_SIZE        );
}